package com.listener;

import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import java.util.LinkedList;

/**
 * version 1.0.0
 * 异步接收消息的监听器，实现MessageListener接口
 * @author KKW
 * @date 2017/7/9.
 */
public class MyMessageListener implements MessageListener{
    public int messageNum;
    public LinkedList<String> messageList;

    public MyMessageListener() {
        this.messageList = new LinkedList<String>();
        messageNum=0;
    }

    public String fetchAMessage(){
        if(messageNum<1) return null;
        messageNum--;
        return messageList.removeFirst();
    }

    @Override
    public void onMessage(Message message) {
        if(message instanceof TextMessage){
            try {
                messageList.addLast(((TextMessage) message).getText());
                messageNum++;
            }catch (Exception e){
                e.printStackTrace();
            }
        }else {
            messageList.addLast("消息格式不正确，未正确接收。。");
            messageNum++;
        }
    }
}
